/**
 * Created by John on 2016/6/13.
 */
var vowelArr = "aeiou";
var consonantArr = "bcdfghjklmnpqrstvwxyz";
var words = getWords();

var wordArr = words.split(/\s|\.|,|!/);
var items = [];

for (var i = 0; i < wordArr.length; i++) {
    try {
        var word = wordArr[i].toLowerCase();
        var vowelCnt = ('|' + word + '|').split(/[aeiou]/i).length - 1;
        var consonantCnt = ('|' + word + '|').split(/[bcdfghjklmnpqrstvwxyz]/i).length - 1;

        var letters = [];
        var vowels = [];
        var consonants = [];
        var other = [];

        for (var j = 0; j < word.length; j++) {
            var ch = word[j];
            if (letters.indexOf(ch) === -1) {
                letters.push(ch);
            }
            if (vowelArr.indexOf(ch) !== -1) {
                if (vowels.indexOf(ch) === -1) {
                    vowels.push(ch);
                }
            } else if (consonantArr.indexOf(ch) !== -1) {
                if (consonants.indexOf(ch) === -1) {
                    consonants.push(ch);
                }
            } else {
                if (other.indexOf(ch) === -1) {
                    other.push(ch);
                }
            }
        }

        var charsets = [];
        if (consonants.length) {
            charsets.push({type: "consonants", chars: consonants});
        }
        if (vowels.length) {
            charsets.push({type: "vowels", chars: vowels});
        }
        if (other.length) {
            charsets.push({type: "other", chars: other});
        }

        var item = {
            word: word,
            first: word[0],
            last: word[word.length - 1],
            size: word.length,
            letters: letters,
            stats: {vowels: vowelCnt, consonants: consonantCnt},
            charsets: charsets
        };
        if (other.length) {
            item.otherChars = other;
        }
        items.push(item);
    } catch (e) {
        console.log(e);
    }
}

var mongo = new Mongo('localhost');
var db = mongo.getDB('BeginningMongoDB');
var collection = db.getCollection('word_stats');
collection.remove({});
collection.ensureIndex({word: 1}, {unique: true});
print(items.length);
for (var item in items) {
    if (items[item].word !== "") {
        collection.insert(items[item]);
    }
}
print(collection.find().count());

function getWords() {
    return "The be and of to it I can't shouldn't say middle-class apllogy till." +
        "Success is getting what you want happiness is wanting what you get." +
        "What youth deemed crystal age finds out was dew." +
        "Your inexperience is a asset and will allow you to think in origin and inconventional ways." +
        "I don’t run away from a challenge because." +
        "I am afraid Instead." +
        "I run towards it because the only way to escape ear is to trample it beneath your foot." +
        "It is lost labor to sow where there is no soil." +
        "Life is painting a picture not doing a sum." +
        "Diligence is the mother of good fortune." +
        "Travel imparts new vigor to the mind." +
        "Risk comes from not knowing what you're doing." +
        "New document." +
        "Truth needs no colour, beauty, no pencil." +
        "The best preparation for tomorrow is doing your best today." +
        "We will not go quietly into the night!" +
        "Miracles happen every day." +
        "A day is a miniature of eternity." +
        "Love means never having to say you're sorry." +
        "What youth deemed crystal, age finds out was dew.";
}